thin.imports('/public/configure/conf.project.js');
thin.imports('/public/plug/laytpl/laytpl.js');
thin.imports('/public/plug/LoadEffect/LoadEffect.js');

thin.use(function() {
	// 頁面加載完畢
	thin.effect();

	// 先獲取html
	volistTPL = $('#projects-volist-template').html();

	// 初始化
	initProjectLists();

	// 綁定搜索按鈕
	$('#searchBtn').on('click', searchProjects);

	// 綁定取消搜索按鈕
	$('#searchClear').on('click', cancelProjects);

	// 綁定條件按鈕
	$('#tpl-condition').on('click', 'a', conditionSearch);
});

var volistTPL,
	data_opts = {
		'round': '',
		'industry': '',
		'city': ''
	}, // 记录opts的数据
	data_opts_index = 0, // 记录opts的次数
	__getdata_opts = {},
	__getdata_provolist = {};

// 初始化
function initProjectLists() {

	// 讀取opts
	thin.imports('/public/configure/conf.opts.js', function() {
		__getdata_opts.name = 'round';
		factory.request('_common_opts', ['round', '融资轮次']);
		__getdata_opts.name = 'industry';
		factory.request('_common_opts', ['industry', '项目领域']);
		__getdata_opts.name = 'city';
		factory.request('_common_opts', ['city', '城市']);
	});

	// 獲取列表
	getProjectVolist(1);

}

// 讀取opts回調
function __complete_opts(res, unkn) {
	data_opts_index = data_opts_index + 1;
	if(res.status == 200) {
		data_opts[unkn[0]] = [res.data, unkn[1], unkn[0]];
	} else {
		// nothing
	}
	// 判断是否全部读取了opts信息
	if(data_opts_index == 3) {
		// 渲染視圖
		showView();
	}
}

// 渲染視圖
function showView() {
	var template = $('#tpl-condition-template').html(),
		tpl = laytpl(template);
	$('#tpl-condition').html(tpl.render(data_opts)).show();
}

// 獲取列表
function getProjectVolist(s) {
	layer.load(0, {
		shade: [0.08, '#000']
	});

	// 獲取項目列表
	factory.request('_project_volist', s);
}

// 獲取項目列表回調
function __complete_provolist(res, unkn) {
	layer.closeAll('loading');
	if(res.status == 200) {
		if(res.data.list.length > 0) {
			createVolist(res.data, unkn);
			$('.nothing-box').hide();
		} else {
			// 暫無内容
			$('#tpl-projects-volist').html('').hide();
			$('.nothing-box').show();
		}
	} else {
		layer.msg(res.error, {
			icon: 2,
			time: 800
		});
	}
}

// 創建列表
function createVolist(data, s) {
	var tpl = laytpl(volistTPL);
	if(s == 1) {
		$('#tpl-projects-volist').html(tpl.render(data.list)).show();
	} else {
		$('#tpl-projects-volist').append(tpl.render(data.list));
	}
}

// 搜索項目
function searchProjects() {
	__getdata_provolist['skey'] = $('input[name=skey]').val();
	if(__getdata_provolist['skey'] != '') {
		getProjectVolist(1);
		$('#searchClear').css('visibility', 'visible');
	}
}

// 取消搜索
function cancelProjects() {
	__getdata_provolist['skey'] = '';
	$('input[name=skey]').val('');
	getProjectVolist(1);
	$('#searchClear').css('visibility', 'hidden');
}

// 篩選項目
function conditionSearch() {
	var that = $(this),
		k = that.attr('data-condition'),
		t = that.attr('data-title'),
		parent = that.parent();
	if(!parent.hasClass('active')) {
		parent.addClass('active').siblings().removeClass('active');
		__getdata_provolist[k] = t;
		getProjectVolist(1);
	}
}